//
//
//
//
//  Created by lbxia on 15/10/21.
//  Copyright © 2015年 lbxia. All rights reserved.
//

#import "QQLBXScanViewController.h"
#import "ScanResultViewController.h"
#import "LBXScanVideoZoomView.h"
#import <AudioToolbox/AudioToolbox.h>

#define kLBXImageNamed(A) [UIImage imageNamed:[@"CodeScan.bundle/" stringByAppendingString:(A)]]

@interface QQLBXScanViewController ()
@property (nonatomic, strong) UILabel *topTitle;
@property (nonatomic, strong) UIView *bottomItemsView;
@property (nonatomic, strong) UIButton *btnPhoto;
@property (nonatomic, strong) UIButton *btnFlash;
@end

@implementation QQLBXScanViewController

- (instancetype)init {
    self = [super init];
    if (self) {
        self.libraryType = SLT_ZXing;
        
        LBXScanViewStyle *style = [[LBXScanViewStyle alloc]init];
        style.centerUpOffset = 44;
        style.photoframeAngleStyle = LBXScanViewPhotoframeAngleStyle_Inner;
        style.photoframeLineW = 2;
        style.photoframeAngleW = 18;
        style.photoframeAngleH = 18;
        style.isNeedShowRetangle = YES;
        style.anmiationStyle = LBXScanViewAnimationStyle_LineMove;
        style.colorAngle = [UIColor colorWithRed:0./255 green:200./255. blue:20./255. alpha:1.0];
        style.animationImage = kLBXImageNamed(@"qrcode_Scan_weixin_Line");
        
        self.style = style;
    }
    return self;
}

-(BOOL)forceEnableInteractivePopGestureRecognizer {
    return YES;
}

-(void)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    self.view.backgroundColor = [UIColor blackColor];
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem qmui_backItemWithTitle:NSLocalizedString(@"nav_back", nil) target:self action:@selector(backAction:)];
    
    self.title = NSLocalizedString(@"btn_scan_dev_code", nil);
    
    //设置扫码后需要扫码图像
    self.isNeedScanImage = YES;
    self.cameraInvokeMsg = NSLocalizedString(@"msg_starting_camera", nil);
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [self drawBottomItems];
    [self drawTitle];
    [self.view bringSubviewToFront:_topTitle];
}

//绘制扫描区域
- (void)drawTitle {
    if (!_topTitle) {
        self.topTitle = [[UILabel alloc]init];
        _topTitle.bounds = CGRectMake(0, 0, 145, 60);
        _topTitle.center = CGPointMake(CGRectGetWidth(self.view.frame)/2, 50);
        
        //3.5inch iphone
        if ([UIScreen mainScreen].bounds.size.height <= 568 ) {
            _topTitle.center = CGPointMake(CGRectGetWidth(self.view.frame)/2, 38);
            _topTitle.font = [UIFont systemFontOfSize:14];
        }
        
        _topTitle.textAlignment = NSTextAlignmentCenter;
        _topTitle.numberOfLines = 0;
        _topTitle.text = NSLocalizedString(@"msg_auto_scan", nil);
        _topTitle.textColor = [UIColor whiteColor];
        [self.view addSubview:_topTitle];
    }    
}

- (void)drawBottomItems {
    if (_bottomItemsView) {
        return;
    }
    CGFloat contentMinY = self.qmui_navigationBarMaxYInViewCoordinator;
    self.bottomItemsView = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.qmui_height - contentMinY - 120, self.view.qmui_width, 120)];
    _bottomItemsView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
    
    [self.view addSubview:_bottomItemsView];
    
    CGSize size = CGSizeMake(65, 87);
    self.btnFlash = [[UIButton alloc]init];
    _btnFlash.bounds = CGRectMake(0, 0, size.width, size.height);
    _btnFlash.center = CGPointMake(CGRectGetWidth(_bottomItemsView.frame)/3, CGRectGetHeight(_bottomItemsView.frame)/2);
     [_btnFlash setImage:kLBXImageNamed(@"qrcode_scan_btn_flash_nor") forState:UIControlStateNormal];
    [_btnFlash addTarget:self action:@selector(openOrCloseFlash) forControlEvents:UIControlEventTouchUpInside];
    
    self.btnPhoto = [[UIButton alloc]init];
    _btnPhoto.bounds = _btnFlash.bounds;
    _btnPhoto.center = CGPointMake(CGRectGetWidth(_bottomItemsView.frame) * 2 / 3, CGRectGetHeight(_bottomItemsView.frame)/2);
    [_btnPhoto setImage:kLBXImageNamed(@"qrcode_scan_btn_photo_nor") forState:UIControlStateNormal];
    [_btnPhoto setImage:kLBXImageNamed(@"qrcode_scan_btn_photo_down") forState:UIControlStateHighlighted];
    [_btnPhoto addTarget:self action:@selector(openPhoto) forControlEvents:UIControlEventTouchUpInside];
    
    [_bottomItemsView addSubview:_btnFlash];
    [_bottomItemsView addSubview:_btnPhoto];
}

- (void)scanResultWithArray:(NSArray<LBXScanResult*>*)array {
    
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    
    LBXScanResult *scanResult = nil;
    for (LBXScanResult *result in array) {
        if ([NSString dd_checkBarCode:result.strScanned]) {
            scanResult = result;
            break;
        }
    }
    
    if (scanResult) {
        self.scanImage = scanResult.imgScanned;
        
        ScanResultViewController *vc = [ScanResultViewController new];
        vc.imgScan = scanResult.imgScanned;
        vc.strScan = scanResult.strScanned;
        vc.strCodeType = scanResult.strBarCodeType;
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        WS(ws);
        [KKAlert warningWithText:NSLocalizedString(@"tips_barcode_recognize_failed", nil) ok:^{
            [ws reStartDevice];
        }];
    }
}

#pragma mark -底部功能项
//打开相册
- (void)openPhoto {
    [self openLocalPhoto:NO];
}

//开关闪光灯
- (void)openOrCloseFlash {
    [super openOrCloseFlash];
   
    if (self.isOpenFlash) {
        [_btnFlash setImage:kLBXImageNamed(@"qrcode_scan_btn_flash_down") forState:UIControlStateNormal];
    } else {
        [_btnFlash setImage:kLBXImageNamed(@"qrcode_scan_btn_flash_nor") forState:UIControlStateNormal];
    }
}


#pragma mark -底部功能项

@end
